from concurrent import futures
import time
import grpc
import hello_pb2
import hello_pb2_grpc
import json
# 实现 proto 文件中定义的 GreeterServicer
class Greeter(hello_pb2_grpc.GreeterServicer):
    # 实现 proto 文件中定义的 rpc 调用
    def SayHello(self, request, context):
        return hello_pb2.HelloReply(message = 'hello {msg}'.format(msg = request.name))
    
    def SayHelloAgain(self, request, context):
        return hello_pb2.HelloReply(message='hello {msg}'.format(msg = request.name))

class Gretera(hello_pb2_grpc.GreeteraServicer):
    def SayStudent(self,request,context):
        print(request.id)
        if request.id=="0":
            c=hello_pb2.Student(msg=json.dumps({"name":"owen","age":22,"sex":"男"}))
        else:
            c=hello_pb2.Student(msg=json.dumps({"name":"lihui","age":23,"sex":"女"}))
        return c
def serve():
    # 启动 rpc 服务
    server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
    hello_pb2_grpc.add_GreeterServicer_to_server(Greeter(), server)
    hello_pb2_grpc.add_GreeteraServicer_to_server(Gretera(),server)
    server.add_insecure_port('[::]:50052')
    server.start()
    try:
        while True:
            time.sleep(60*60*24) # one day in seconds
    except KeyboardInterrupt:
        server.stop(0)
if __name__ == '__main__':
    serve()